package org.dizena.modules.master.web;

import org.dizena.base.avo.req.JqGridReq;
import org.dizena.base.avo.resp.JqGridResp;
import org.dizena.modules.master.bean.Role;
import org.dizena.modules.master.service.RoleService;
import org.dizena.utils.ObjectUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("master/role")
public class MasterRoleController
{

    @Resource
    private RoleService roleService;

    @GetMapping("list")
    public String list(Model m)
    {
        return "master/role/list";
    }

    @PostMapping("list")
    @ResponseBody
    public JqGridResp<Role> list(JqGridReq req)
    {
        return roleService.list(req);
    }

    @PostMapping("crud")
    @ResponseBody
    public String crud(String oper, Role role)
    {
        return roleService.crud(oper, role);
    }

    @PostMapping("upress")
    public String upress(String id, String s)
    {
        Role r = new Role();
        r.setId(id);
        r.setRessIds(ObjectUtil.str2List(s, ","));
        roleService.crud("edit", r);
        return "master/role/list";
    }

    @GetMapping("ress/{id}")
    public String ress(@PathVariable("id") String id, Model m)
    {
        m.addAttribute("m", roleService.get(id));
        return "master/role/edit";
    }

    @PostMapping("ress/{id}")
    @ResponseBody
    public List<Map<String, Object>> ress(@PathVariable("id") String id)
    {
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        lists.add(roleService.getRoleRess(id));
        return lists;
    }

}
